package com.peas.platform.module.icims.contract.initial.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.common.core.constant.Constants;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.component.ApproveCallback;
import com.peas.platform.module.icims.base.approve.dto.ApproveDto;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderNodeDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderBusinessType;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderService;
import com.peas.platform.module.icims.base.approve.service.ApproveService;
import com.peas.platform.module.icims.contract.initial.controller.vo.ContractInitialBorrowVo;
import com.peas.platform.module.icims.contract.initial.domain.ContractInitialBorrowDomain;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialBorrowDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialDto;
import com.peas.platform.module.icims.contract.initial.enums.ContractInitialBorrowState;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialBorrowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ContractInitialBorrowServiceImpl extends BaseServiceImpl<ContractInitialBorrowDto> implements ContractInitialBorrowService, ApproveCallback {

    @Resource
    private ContractInitialBorrowDomain contractInitialBorrowDomain;
    @Resource
    private ApproveService approveService;
    @Resource
    private ApproveOrderService approveOrderService;

    @Override
    public BaseDomain<ContractInitialBorrowDto, ? extends BasePo> getDomain() {
        return contractInitialBorrowDomain;
    }

    @Transactional
    @Override
    public void borrowContractInitial(ContractInitialDto dto, Long approveId, List<ApproveOrderNodeDto> approveOrderNodes, List<ContractInitialBorrowVo> items) {
        ApproveDto ad = approveService.getById(approveId);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractBorrow;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        items.forEach(v -> {
            ContractInitialBorrowDto item = BeanUtil.copyProperties(v, ContractInitialBorrowDto.class);
            item.setState(ContractInitialBorrowState.submit.getKey());
            this.save(item);
            String desc = "合同编码(" + dto.getCode() + ")";
            approveOrderService.createApproveOrder(ad, approveOrderNodes, approveOrderBusinessType, item.getId(), desc, null, this.getClass());
        });
    }

    @Transactional
    @Override
    public void revocationContractInitialBorrow(Long id) {
        ContractInitialBorrowDto dto = this.getById(id);
        Assert.notNull(dto, "找不到数据");
        Assert.equals(dto.getState(), ContractInitialBorrowState.submit.getKey(), "单据状态不是[{}]", ContractInitialBorrowState.submit.getValue());
        dto.setState(ContractInitialBorrowState.revocation.getKey());
        this.updateById(dto);
        ApproveOrderBusinessType approveOrderBusinessType = ApproveOrderBusinessType.contractBorrow;
        Assert.notNull(approveOrderBusinessType, "合同数据错误");
        approveOrderService.revocationApproveOrder(approveOrderBusinessType, id);
    }

    @Transactional
    @Override
    public void returnContractInitialBorrow(Long contractInitialBorrowId) {
        ContractInitialBorrowDto dto = this.getById(contractInitialBorrowId);
        Assert.notNull(dto, "找不到合同借阅记录");
        Assert.equals(dto.getState(), "3", "合同借阅记录的借阅状态不是[已借出(待归还)]");
        dto.setState(ContractInitialBorrowState.returned.getKey());
        dto.setReturnTime(DateTime.now());
        this.updateById(dto);
    }

    @Transactional
    @Override
    public void callbackAfterApprove(Long businessId, boolean isPass, boolean isFinalPass) {
        if (businessId == null)
            return;
        if (isPass) {
            if (isFinalPass) {
                ContractInitialBorrowDto dto = this.getById(businessId);
                Assert.notNull(dto, "找不到合同借阅记录");
                dto.setState(ContractInitialBorrowState.pass.getKey());
                // 纸质借出
                if (StrUtil.equals(dto.getBorrowType(), "1")) {
                    dto.setBorrowToken(Long.toHexString(Constants.snowflake.nextId()));
                }
                this.updateById(dto);
            }
        } else {
            ContractInitialBorrowDto dto = this.getById(businessId);
            dto.setState(ContractInitialBorrowState.reject.getKey());
            this.updateById(dto);
        }
    }
}
